/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.aurelius.relations;

import de.aurelius.SimplePojo;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.testng.annotations.Test;

/**
 *
 * @author Lukasz
 */
public class TransistorOneToManyExample {

    @Test
    public void testOneToManyRelations() {
        EntityManagerFactory emf =
                Persistence.createEntityManagerFactory("embedded-hsql-persistance-unit");
        EntityManager em = emf.createEntityManager();

        em.getTransaction().begin();

        Transistor transistor = new Transistor();
        transistor.setId(1);
        transistor.setMiniaturized(false);
        transistor.setName("bipolar");

        Transistor transistor2 = new Transistor();
        transistor2.setId(2);
        transistor2.setMiniaturized(true);
        transistor2.setName("unipolar");

        Microprocessor microprocessor = new Microprocessor();
        microprocessor.setCommercialName("Athlon");
        microprocessor.setId(1);
        microprocessor.setProducent("AMD");

        em.persist(microprocessor);

        transistor.setMicroprocessor(microprocessor);
        em.persist(transistor);
        em.persist(transistor2);

        em.getTransaction().commit();

        em.getTransaction().begin();

        Query createQuery = em.createQuery("from Transistor");
        List resultList = createQuery.getResultList();

        for (Object object : resultList) {
            System.out.println(object);
        }

        Query createQuery2 = em.createQuery("from Microprocessor");
        List resultList2 = createQuery2.getResultList();

        for (Object object : resultList2) {
            Microprocessor microprocessor2 = (Microprocessor) object;
            microprocessor2.getTransistors();
            System.out.println(microprocessor2);
        }

        em.getTransaction().commit();
        em.close();
        emf.close();
    }
}
